App.Notes = angular.module('NotesGroup', []).
controller('NotesGroup.Ctrl', [function () {

}]).
service('NotesGroup.ConfigService', ['MainApp.ConfigService', function (Config) {
	this.templateDir = Config.Template.templateDir+'NotesGroup/';
}]).
factory('NotesGroup.Factory', ['Tools.Service', '$q', 'Note.Factory', 'MainApp.ConfigService', function (Tools, $q, Note, Config) {
	function NotesGroup () {
		var that = this,
			notes = [];
		function addNote (note) {
			notes.push(note);
		}
		function removeNote (id) {
			var i, l;
			for(i=0, l=notes.length; i<l; ++i) {
				if(notes[i].getId() == id) {
					notes.splice(i, 1);
					break;
				}
			}
		}
		function getCount () {
			return notes.length;
		}
		function toObject () {
			return Tools.map(notes, function (note) {
				return note.toObject();
			});
		}
		(function construct () {
			that.addNote = addNote;
			that.toObject = toObject;
			that.getCount = getCount;
			that.removeNote = removeNote;
		})();
	}
	NotesGroup.getByIds = function (ids) {
		var d = $q.defer(),
			notes = new NotesGroup();
		Tools.map(ids, function (id) {
			if(id == Config.Api.emptyItem) {
				d.resolve(notes);
				return true;
			}
			Note.getById(id).then(
				function (note) {
					notes.addNote(note);
					if(notes.getCount() == ids.length) {
						d.resolve(notes);
					}
				},
				function (error) {
					d.reject(error);
				}
			);
		});
		return d.promise;
	}
	return NotesGroup;
}]).
directive('notesGroupList', ['NotesGroup.ConfigService', 'MainApp.ConfigService', function (NotesGroupConfig, Config) {
	return {
		restrict: 'E',
		templateUrl: NotesGroupConfig.templateDir+'list'+Config.Template.directiveExt,
		replace: true,
		scope: {
			notes: '=data'
		},
		controller: function ($scope) {
			$scope.actualNote = {
				title: 'Ala'
			}
		}
	}
}]);